package com.wslds.blog.action.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.wslds.base.action.AbstractCRUDAction;
import com.wslds.base.util.BlogCons;
import com.wslds.base.util.SessionUtil;
import com.wslds.bean.Type;
import com.wslds.blog.service.TypeService;

@SuppressWarnings("serial")
@Component("typeAdminAction")
@Scope("prototype")
public class TypeAction extends AbstractCRUDAction<Type, TypeService> {

	public String refreshTypeCache() {

		Type typeParam = new Type();
		List<Type> typeList = service.find("type", typeParam);
		Map<Long, Type> typeMap = new HashMap<Long, Type>();
		for (Type type : typeList) {
			typeMap.put(type.getId(), type);
		}
		SessionUtil.getServletContext().setAttribute(BlogCons.APPLICATION.TYPE,
				typeMap);

		return SUCCESS;
	}

	public String insert() {
		String result = super.insert();
		SessionUtil.refreshSidebar();
		return result;
	}

	public String update() {
		String result = super.update();
		SessionUtil.refreshSidebar();
		return result;
	}

	@Override
	@Resource(type = TypeService.class)
	public void setService(TypeService service) {
		this.service = service;
	}

	@Override
	public String getInstanceName() {
		return "type";
	}

}
